#include "SpaceInvaders.h"

EnemyCollection::EnemyCollection()
{
	isActive = true;

	initialPosition.XValue = 200;
	initialPosition.YValue = 40;

	changeDirection = false;
	movingRight = true;
	landed = false;

	timeToFire = rand()%40;

	incrementPosition = initialPosition;

	for (int i = 0; i < 4; i++)
	{
		for (int k = 0; k < 10; k++)
		{
			theEnemy[i].row = i;
			theEnemy[i].column = k;
		}
	}	

	for (int i = 0; i < 40; i++)
	{
		theEnemy[i].isActive = true;

		if (i == 0)
		{
			theEnemy[i].thePosition = initialPosition;
		}
		else if (i == 10 || i == 20|| i == 30)
		{
			incrementPosition.XValue = initialPosition.XValue;
			incrementPosition.YValue += 60;
		}
		else
		{
			incrementPosition.XValue += 60;
		}
		
		theEnemy[i].thePosition = incrementPosition;

		theEnemy[i].enemySize.XValue = 40;
		theEnemy[i].enemySize.YValue = 40;

		theEnemy[i].theShipImage.init(theEnemy[i].thePosition,theEnemy[i].enemySize,
			"SpaceInvaders/Invader1.jpg");
	}
}

void EnemyCollection::update(Vector2D bulletPos[3], double &timeToMove)
{	
	
	if (changeDirection)
	{
		for (int i = 0; i < 40; i++)
		{
			theEnemy[i].thePosition.YValue += 10;
		}
		
		if (timeToMove > 0.01)
		{
			timeToMove = timeToMove - 0.005;
		}

		changeDirection = false;
	}
	else
	{	
		if (movingRight)
		{
			for (int i = 0; i < 40; i++)
			{
				theEnemy[i].thePosition.XValue += 10;

				if (theEnemy[i].thePosition.XValue >= SCREENWIDTH - 40 && theEnemy[i].isActive)
				{
					changeDirection = true;
				}
			}
		}
		else
		{
			for (int i = 0; i < 40; i++)
			{
				theEnemy[i].thePosition.XValue -= 10;

				if (theEnemy[i].thePosition.XValue <= 40 && theEnemy[i].isActive)
				{
					changeDirection = true;
				}
			}
		}

		if (changeDirection && movingRight)
		{
			movingRight = false;
		}
		else if (changeDirection && !movingRight)
		{
			movingRight = true;
		}

		for (int i = 0; i < 40; i++)
		{
			if (theEnemy[i].thePosition.YValue < 10)
			{
				landed = true;
			}
		}
	}
	


}

void EnemyCollection::draw()
{
	for (int i = 0; i < 40; i++)
	{
		theEnemy[i].theShipImage.setPosition(theEnemy[i].thePosition);
		theEnemy[i].theShipImage.Draw();
	}
	
}

bool EnemyCollection::checkLanded()
{
	if (landed)
		return true;
	else
		return false;
}

void SpaceInvadersGame::GameInit()
{
	setGameActive(true);

	theState = STARTUP;

	startButton.Initialise(SCREENHEIGHT - 100, SCREENWIDTH - 370, SCREENHEIGHT - 50, SCREENWIDTH - 220, "START");

	endButton.Initialise(SCREENHEIGHT - 100, SCREENWIDTH - 200, SCREENHEIGHT - 50, SCREENWIDTH - 50, "END");

	menuButton.Initialise(SCREENHEIGHT/2 - 100, SCREENWIDTH/2 - 200, SCREENHEIGHT/2 - 50, SCREENWIDTH/2 - 50, "MENU");

	gTheMouse.Initialise(SCREENHEIGHT / 2, SCREENWIDTH / 2);

	thePlayer.thePosition.XValue = SCREENWIDTH/2;
	thePlayer.thePosition.YValue = SCREENHEIGHT - 20;

	thePlayer.theShip.init(thePlayer.thePosition,20,60);
	thePlayer.theGun.init(Vector2D(thePlayer.thePosition.XValue,thePlayer.thePosition.YValue-10),15,15);

	for (int i = 0; i < BulletAmount; i++)
	{
		thePlayer.BulletActive[i] = false;

		thePlayer.BulletShape[i].init(thePlayer.theGun.getPosition(),5,10);
	}
	
	thePlayer.BulletVel.XValue = 0;
	thePlayer.BulletVel.YValue = -4;

	timer = 0;

	score = 0;

	highScore = 0;//change this later

	Won = false;

	timeToMove = 0.12;
	timeToNextMove = timeToMove;

}

void SpaceInvadersGame::GameMain()
{
	if (theState == STARTUP)
	{	
		startButton.Update(gTheMouse);
		endButton.Update(gTheMouse);

		startButton.Draw();
		endButton.Draw();

		gTheMouse.Update(SCREENWIDTH, SCREENHEIGHT);
		gTheMouse.Draw();
		
		if(startButton.getActive())
		{
			theState = INGAME;
		}
	
		if (endButton.getActive())
		{
			GameShutdown();
		}

	}
	else if (theState == INGAME)
	{

		if (theClock.ticker(timeToNextMove))
		{
			timeToNextMove -= 0.01;

			if(theEnemies.checkLanded())
			{
				theState = WRAPUP;
				Won = false;
			}
		}
		
		theGameLoop();

	}
	else if (theState == WRAPUP)
	{
		menuButton.Update(gTheMouse);
		menuButton.Draw();

		gTheMouse.Update(SCREENWIDTH, SCREENHEIGHT);
		gTheMouse.Draw();
	}
}



void SpaceInvadersGame::GameShutdown()
{
	setGameActive(false);

	theResult.score = highScore;
}

void SpaceInvadersGame::theGameLoop()
{

	if (timeToNextMove <= 0)
	{
		theEnemies.update(thePlayer.BulletPos, timeToMove);
		timeToNextMove = timeToMove;
	}

	if (KEYPRESSED('A') && thePlayer.theVelocity.XValue > -10)
	{
		thePlayer.theVelocity.XValue = thePlayer.theVelocity.XValue - (float)0.25;
	}
	else if (KEYPRESSED('D') && thePlayer.theVelocity.XValue < 10)
	{
		thePlayer.theVelocity.XValue = thePlayer.theVelocity.XValue + (float)0.25;
	}
	else
	{
		if (thePlayer.theVelocity.XValue > 0)
		{
			thePlayer.theVelocity.XValue = thePlayer.theVelocity.XValue - (float)0.25;
		}	
		else if (thePlayer.theVelocity.XValue < 0)
		{
			thePlayer.theVelocity.XValue = thePlayer.theVelocity.XValue + (float)0.25;
		}
	}

	if (thePlayer.thePosition.XValue + thePlayer.theVelocity.XValue > SCREENWIDTH - 
		(thePlayer.theShip.getWidth()/2) ||thePlayer.thePosition.XValue + 
		thePlayer.theVelocity.XValue < 0 + (thePlayer.theShip.getWidth()/2))
	{
		thePlayer.theVelocity.XValue = 0;
	}

	thePlayer.thePosition += thePlayer.theVelocity;

	thePlayer.theShip.Update(thePlayer.theVelocity);
	thePlayer.theGun.Update(thePlayer.theVelocity);

	if (KEYPRESSED(VK_LSHIFT) && theClock.ticker())
	{
		bool canFire = false;
		int fireThis = -1;

		for (int i = 0; i < BulletAmount; i++)
		{
			if (!thePlayer.BulletActive[i])
			{
				canFire = true;
				fireThis = i;
			}
		}

		if (canFire)
		{
			thePlayer.BulletPos[fireThis] = thePlayer.theGun.getPosition();
			thePlayer.BulletActive[fireThis] = true;
		}
	}

	for (int i = 0; i < BulletAmount; i++)
	{
		if (thePlayer.BulletActive[i])
		{
			thePlayer.BulletShape[i].Update(thePlayer.BulletVel);
			thePlayer.BulletPos[i] += thePlayer.BulletVel;
			thePlayer.BulletShape[i].Draw();
		}

		if (thePlayer.BulletPos[i].YValue > SCREENHEIGHT)
		{
			thePlayer.BulletActive[i] = false;
		}
	}

	thePlayer.theShip.Draw();
	thePlayer.theGun.Draw();

	theEnemies.draw();
}

Game* GetSpaceInvadersGame()
{
	return new SpaceInvadersGame;
}